--[[ 

  (C) Markus Dittrich, 2012 

  this module contains a several random_gen number generators

--]]


local os = require "os"
local random_gen = require "random_gen"


-- return a generator for uniformly distributed random_gen numbers in 
-- the range [0,1) 
local function uniform(seed) 

   if seed == nil then
      seed = os.time()
   end

   local rand = random_gen.new(seed)

   -- return closure
   return 
     function ()
       return rand()
     end
end



-- return a generator for uniformly distributed random_gen numbers
-- in the range [a,b]
local function uniform_r(a, b, seed)
   
   if seed == nil then
      seed = os.time()
   end

   rand = random_gen.new(seed)

   -- return closure
   return 
     function ()
       return rand(a, b)
     end
end



-- return a generator for normal distributed random_gen numbers
-- with mean and standard deviation. If no values for mean
-- and std are provided a normal distribution with mean 0 and
-- standard deviate 1 is returned
local function normal(mean, std, seed)

   if seed == nil then
      seed = os.time()
   end

   if mean == nil then
     mean = 0
   end

   if std == nil then
     std = 1
   end

   rand = random_gen.new(seed)

   -- return closure
   return 
     function()
       if num1 == nil then
         num1, num2 = rand:normal()
         num = num1
       else
         num = num2
         num1 = nil
       end
            
       return (mean + num*std) 
     end
end



-- return a generator for exponentially distributed random_gen 
-- numbers with rate lambda. If no value for lambda is provided
-- lambda = 1
local function exponential(lambda, seed)

  if seed == nil then
    seed = os.time()
  end

  if lambda == nil then
    lambda = 1
  end

  rand = random_gen.new(seed)

  -- return closure
  return 
    function()
      return rand:exponential(lambda)
    end
end



random = {
  exponential = exponential,
  normal = normal,
  uniform = uniform,
  uniform_r = uniform_r
}

return random
